﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Element.UI
{
    /// <summary>
    /// Element.UI.ElWindow
    /// </summary>
    [TemplatePart(Name = PART_TITLE_PANEL, Type = typeof(Panel))]
    [TemplatePart(Name = PART_TITLE, Type = typeof(ContentPresenter))]
    public class ElWindow : Window
    {
        #region Constants
        private const string PART_TITLE_PANEL = "PART_TITLE_PANEL";
        private const string PART_TITLE = "PART_TITLE";
        #endregion

        #region Fields
        private Panel _titlePanel;
        #endregion

        #region Private Properties
        private Panel TitlePanel
        {
            get { return _titlePanel; }
            set
            {
                if (_titlePanel != null)
                {
                    _titlePanel.MouseLeftButtonDown -= TitlePanel_MouseLeftButtonDown;
                }
                _titlePanel = value;
                if (_titlePanel != null)
                {
                    _titlePanel.MouseLeftButtonDown += TitlePanel_MouseLeftButtonDown;
                }
            }
        }
        #endregion

        #region Dependency Properties
        /// <summary>
        /// 标题水平位置 依赖属性
        /// </summary>
        public static readonly DependencyProperty TitleHorizontalProperty =
            DependencyProperty.Register("TitleHorizontal", typeof(HorizontalAlignment), typeof(ElWindow), new PropertyMetadata(HorizontalAlignment.Stretch));

        /// <summary>
        /// 标题显示状态 依赖属性
        /// </summary>
        public static readonly DependencyProperty IsShowTitleProperty =
            DependencyProperty.Register("IsShowTitle", typeof(bool), typeof(ElWindow), new PropertyMetadata(true));

        /// <summary>
        /// 左边标题工具栏 依赖属性
        /// </summary>
        public static readonly DependencyProperty LeftTitleBarProperty =
            DependencyProperty.Register("LeftTitleBar", typeof(object), typeof(ElWindow), new PropertyMetadata(null));

        /// <summary>
        /// 右边标题工具栏 依赖属性
        /// </summary>
        public static readonly DependencyProperty RightTitleBarProperty =
            DependencyProperty.Register("RightTitleBar", typeof(object), typeof(ElWindow), new PropertyMetadata(null));

        /// <summary>
        /// 标题最小高度 依赖属性
        /// </summary>
        public static readonly DependencyProperty TitleMinHeightProperty =
            DependencyProperty.Register("TitleMinHeight", typeof(double), typeof(ElWindow), new PropertyMetadata(0.0));
        #endregion

        #region Properties

        /// <summary>
        /// 获取或设置 标题水平位置
        /// </summary>
        public HorizontalAlignment TitleHorizontal
        {
            get { return (HorizontalAlignment)GetValue(TitleHorizontalProperty); }
            set { SetValue(TitleHorizontalProperty, value); }
        }

        /// <summary>
        /// 获取或设置 标题显示状态
        /// </summary>
        public bool IsShowTitle
        {
            get { return (bool)GetValue(IsShowTitleProperty); }
            set { SetValue(IsShowTitleProperty, value); }
        }

        /// <summary>
        /// 获取或设置 左边标题工具栏
        /// </summary>
        public object LeftTitleBar
        {
            get { return (object)GetValue(LeftTitleBarProperty); }
            set { SetValue(LeftTitleBarProperty, value); }
        }

        /// <summary>
        /// 获取或设置 右边标题工具栏
        /// </summary>
        public object RightTitleBar
        {
            get { return (object)GetValue(RightTitleBarProperty); }
            set { SetValue(RightTitleBarProperty, value); }
        }

        /// <summary>
        /// 获取或设置 标题最小高度
        /// </summary>
        public double TitleMinHeight
        {
            get { return (double)GetValue(TitleMinHeightProperty); }
            set { SetValue(TitleMinHeightProperty, value); }
        }



        public double TitleFontSize
        {
            get { return (double)GetValue(TitleFontSizeProperty); }
            set { SetValue(TitleFontSizeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TitleFontSize.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TitleFontSizeProperty =
            DependencyProperty.Register("TitleFontSize", typeof(double), typeof(ElWindow), new PropertyMetadata(0.0));

        #endregion

        #region Constructors
        static ElWindow()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ElWindow), new FrameworkPropertyMetadata(typeof(ElWindow)));
            CommandManager.RegisterClassCommandBinding(typeof(ElWindow),
                new CommandBinding(SystemCommands.CloseWindowCommand, OnCloseWindow));
            CommandManager.RegisterClassCommandBinding(typeof(ElWindow),
                new CommandBinding(SystemCommands.MaximizeWindowCommand, OnMaximizeWindow));
            CommandManager.RegisterClassCommandBinding(typeof(ElWindow),
                new CommandBinding(SystemCommands.MinimizeWindowCommand, OnMinimizeWindow));
            CommandManager.RegisterClassCommandBinding(typeof(ElWindow),
                new CommandBinding(SystemCommands.ShowSystemMenuCommand, OnShowSystemMenu));
        }
        #endregion

        #region Routed Command handlers
        private static void OnShowSystemMenu(object sender, ExecutedRoutedEventArgs e)
        {
            ElWindow window = sender as ElWindow;
            if (window == null) return;

            SystemCommands.ShowSystemMenu(window, new Point(window.Left, window.Top));
        }

        private static void OnMinimizeWindow(object sender, ExecutedRoutedEventArgs e)
        {
            ElWindow window = sender as ElWindow;
            if (window == null) return;

            if(window.WindowState == WindowState.Minimized)
            {
                SystemCommands.RestoreWindow(window);
            }
            else
            {
                SystemCommands.MinimizeWindow(window);
            }
        }

        private static void OnMaximizeWindow(object sender, ExecutedRoutedEventArgs e)
        {
            ElWindow window = sender as ElWindow;
            if (window == null) return;

            if(window.WindowState == WindowState.Maximized)
            {
                SystemCommands.RestoreWindow(window);
            }
            else
            {
                SystemCommands.MaximizeWindow(window);
            }
        }

        private static void OnCloseWindow(object sender, ExecutedRoutedEventArgs e)
        {
            ElWindow window = sender as ElWindow;
            if (window == null) return;

            SystemCommands.CloseWindow(window);
        }

        #endregion

        #region Event Handlers

        private void TitlePanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
        #endregion

        #region Override Methods
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this.TitlePanel = GetTemplateChild(PART_TITLE_PANEL) as Panel;
        }
        #endregion
    }
}
